package com.hengtongxing.hejiayun.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseAdapter_Recycler;
import com.hengtongxing.hejiayun.base.BaseViewHolder;
import com.hengtongxing.hejiayun.utils.GlideUtils;
import com.hengtongxing.hejiayun.widget.RoundImageView;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * Time: 2019/11/5
 * Author: ZF
 * Description:
 */

public class Adapter_PictureSelector extends BaseAdapter_Recycler {
    public static final int TYPE_ADD_PIC = 1;//添加照片
    public static final int TYPE_SHOW_PIC = 2;//
    private List<LocalMedia> mediaList = null;
    private int selectMax = 9;
    private OnAddPicClickListener onAddPicClickListener;
    private OnItemClickListener mItemClickListener;

    private List<String> imgList;

    public Adapter_PictureSelector(Context context) {
        super(context, R.layout.item_picture_selector);
    }

    public void setImgList(List<String> imgList) {
        this.imgList = imgList;
    }

    public void setOnAddPicClickListener(OnAddPicClickListener onAddPicClickListener) {
        this.onAddPicClickListener = onAddPicClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }


    public void setList(List<LocalMedia> mediaList) {
        this.mediaList = mediaList;
        notifyDataSetChanged();
    }

    public void addMediaList(List<LocalMedia> list) {
        if (mediaList == null) {
            mediaList = new ArrayList<>();
        }
        if (list != null) {
            mediaList.addAll(list);
        }
        notifyDataSetChanged();
        setImgData();
    }


    public List<LocalMedia> getMediaList() {
        return mediaList;
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    @Override
    public int getItemCount() {
        if (mediaList == null) {
            return 1;
        } else if (mediaList.size() < selectMax) {
            return mediaList.size() + 1;
        } else {
            return mediaList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_ADD_PIC;
        } else {
            return TYPE_SHOW_PIC;
        }
    }

    private boolean isShowAddItem(int position) {
        if (mediaList == null) {
            return true;
        }
        int size = mediaList.size();
        return position == size;
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_picture_selector, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        onBindingHolder((ViewHolder) holder, position);
    }

    private void onBindingHolder(ViewHolder holder, int position) {

        if (getItemViewType(position) == TYPE_ADD_PIC) {
            holder.iv_picture.setVisibility(View.GONE);
            holder.tv_delete.setVisibility(View.GONE);
            holder.layout_add_picture.setVisibility(View.VISIBLE);
            holder.layout_add_picture.setOnClickListener(v -> {
                if (onAddPicClickListener != null) {
                    onAddPicClickListener.onAddPicClick();
                }
            });
            return;
        }
        holder.iv_picture.setVisibility(View.VISIBLE);
        holder.layout_add_picture.setVisibility(View.GONE);
        holder.tv_delete.setVisibility(View.VISIBLE);

        holder.tv_delete.setOnClickListener(v -> {
            int index = holder.getAdapterPosition();
            // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
            // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
            if (index != RecyclerView.NO_POSITION) {
                mediaList.remove(index);
                notifyItemRemoved(index);
                notifyItemRangeChanged(index, mediaList.size());
            }
            setImgData();
        });

        LocalMedia media = mediaList.get(position);
//        int mimeType = media.getMimeType();
        String path = media.getPath();
//        if (media.isCut() && !media.isCompressed()) {
//            path = media.getCutPath();// 裁剪过
//        } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
//            path = media.getCompressPath(); // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
//        } else {
//            path = media.getPath();// 原图
//        }

//        int pictureType = PictureMimeType.isPictureType(media.getPictureType());

//        holder.layout_video.setVisibility(pictureType == PictureConfig.TYPE_VIDEO ? View.VISIBLE : View.GONE);
//        if (mimeType == PictureMimeType.ofAudio()) {//音频
//            holder.view_videoFlag.setBackgroundResource(R.drawable.ve_loudspeaker);
//        } else {
//            holder.view_videoFlag.setBackgroundResource(R.drawable.ve_video);
//        }
//        holder.tv_video_duration.setText(DateUtils.timeParse(media.getDuration()));
//
//        if (mimeType == PictureMimeType.ofAudio()) {//音频
//            holder.iv_picture.setImageResource(R.drawable.audio_placeholder);
//        } else {
        GlideUtils.loadImage(holder.itemView.getContext(), holder.iv_picture, path);
//        }

//        if (media.isCompressed()) { // 图片压缩
        //            Log.i("compress image result:", new File(media.getCompressPath()).length() / 1024 + "k");
        //        }
        //        Log.i("org      image result:", new File(media.getPath()).length() / 1024 + "k");
        //itemView 的点击事件
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(v -> {
                mItemClickListener.onItemClick(holder.getAdapterPosition(), v);
            });
        }
    }

    public void setImgData() {
        imgList.clear();
        for (LocalMedia localMedia : mediaList) {
            imgList.add(localMedia.getPath());
        }
    }

    public interface OnAddPicClickListener {
        void onAddPicClick();
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }


    class ViewHolder extends BaseViewHolder {
        View itemView;
        RoundImageView iv_picture;
        TextView tv_delete;
        View layout_video;
        View view_videoFlag;
        View layout_add_picture;
        TextView tv_video_duration;

        ViewHolder(View view) {
            super(view);
            this.itemView = view;
            this.iv_picture = view.findViewById(R.id.iv_picture);
            this.tv_delete = view.findViewById(R.id.tv_delete);
            this.layout_video = view.findViewById(R.id.layout_video);
            this.view_videoFlag = view.findViewById(R.id.view_videoFlag);
            this.tv_video_duration = view.findViewById(R.id.tv_video_duration);
            this.layout_add_picture = view.findViewById(R.id.layout_add_picture);
        }
    }
}
